//
//  Setting.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 9/12/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Setting.h"
#import "SimpleAudioEngine.h"

@implementation Setting
@synthesize _isSoundBG = mIsSoundBG;
@synthesize _isSoundFX = mIsSoundFX;
@synthesize _isCalibrate = mIsCalibrate;

+(Setting *) getInstance
{
    static Setting *mInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mInstance = [[Setting alloc] init];
    });
    
    return mInstance;
}

-(id) init
{
    if (self = [super init])
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([defaults objectForKey:KEY_SETTING_SOUND_BG] == nil)
            self._isSoundBG = YES;
        else
            mIsSoundBG = [defaults boolForKey:KEY_SETTING_SOUND_BG];
        
        if ([defaults objectForKey:KEY_SETTING_SOUND_FX] == nil)
            mIsSoundFX = YES;
        else
            mIsSoundFX = [defaults boolForKey:KEY_SETTING_SOUND_FX];
        
        mIsCalibrate = NO;
    }
    
    return self;
}

-(void) set_isSoundBG:(BOOL)_isSoundBG
{
    CCLOG(@"set sound");
    mIsSoundBG = _isSoundBG;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:mIsSoundBG forKey:KEY_SETTING_SOUND_BG];
    [defaults synchronize];
    
    if (mIsSoundBG)
    {
        if ([[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying])
        {
            [[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
        }
        else
        {
//            // bgmInGame01.mp3
//            static int a = 0;
//            a = (a % 3) + 1;
//            NSString *string = [NSString stringWithFormat:@"bgmInGame0%d.mp3", a];
            [[SimpleAudioEngine sharedEngine] playBackgroundMusic:SOUND_BG];
        }
    }
    else
    {
        [[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
//        [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
    }
}

-(void) set_isSoundFX:(BOOL)_isSoundFX
{
    mIsSoundFX = _isSoundFX;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:mIsSoundFX forKey:KEY_SETTING_SOUND_FX];
    [defaults synchronize];
}

@end
